/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package commons.util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author Susanta Tewari
 * @version 1.0 Dec 23, 2010
 */
public class GUIUtil {

    /**
     * Constructs ...
     *
     */
    private GUIUtil() {}

    /**
     * Displays a panel in a frame.
     *
     * @param frmTitle the frame title
     * @param component the panel to be displayed
     */
    public static void displayGUI(String frmTitle, JComponent component) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                 | UnsupportedLookAndFeelException e) {
            e.toString();
        }

        JFrame frame = new JFrame(frmTitle) {

            @Override    // makes setMaximumSize work; src from http://stackoverflow.com/a/16411076/225866
            public void paint(Graphics g) {

                Dimension d    = getSize();
                Dimension m    = getMaximumSize();
                boolean resize = (d.width > m.width) || (d.height > m.height);

                d.width  = Math.min(m.width, d.width);
                d.height = Math.min(m.height, d.height);

                if (resize) {

                    Point p = getLocation();

                    setVisible(false);
                    setSize(d);
                    setLocation(p);
                    setVisible(true);
                }

                super.paint(g);
            }
        };

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);    // helps in debugging
        frame.getContentPane().add(component);    // add comp
        frame.pack();
        setSizeAndLocation(frame);    // maxSize and Centered
        frame.setVisible(true);
    }

    public static void setSizeAndLocation(Window frame) {

        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();


        //
        final int screenWidth  = (int) dimension.getWidth();
        final int screenHeight = (int) dimension.getHeight();
        final int maxWidth     = screenWidth - 100;
        final int maxHeight    = screenHeight - 200;
        int frameWidth         = (frame.getWidth() < maxWidth) ? frame.getWidth() : maxWidth;
        int frameHeight        = (frame.getHeight() < maxHeight) ? frame.getHeight() : maxHeight;


        //
        int x = (int) ((screenWidth - frameWidth) / 2.0);
        int y = (int) ((screenHeight - frameHeight) / 4.0);

        frame.setLocation(x, y);
        frame.setMaximumSize(new Dimension(maxWidth, maxHeight));
    }

    /**
     * A popup listener.
     */
    public static class PopupListener extends MouseAdapter {

        private JPopupMenu popup;

        public PopupListener(JPopupMenu popup) {
            this.popup = popup;
        }

        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }

        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }

        private void maybeShowPopup(MouseEvent e) {

            if (e.isPopupTrigger()) {
                popup.show(e.getComponent(), e.getX(), e.getY() + 25);
            }
        }
    }
}
